﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Library;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace SimulationGame
{
    /*
     this class describes the behaviour of the humans that the player control
     */
    public class Human : Sprite
    {
		//State of the Human
		public enum State { Standing, Walking };
		public State currentState = State.Standing;
		public bool facingRight = true;

		//Tasks
		public bool hasTask = false;
        public enum Task { walking = 0, hunting = 0, mining = 0, farming = 0, crafting = 0, fighting = 0, fishing = 0}; // maybe useful
        public Task currentTask;		
		public bool isSelected = false;
		public Vector2 goalPosition;

		//Related to mining
		private List<Tile> tilesToMine = new List<Tile>();
		private Tile goalTile = null;

		//Stats
		public int health = 0;
		public int food = 0;
		public int huntingLevel = 0;
		public int miningLevel = 0;
		public int farmingLevel = 0;
		public int craftingLevel = 0;
		public int fightingLevel = 0;
		public int fishingLevel = 0;

		//Items
		public List<Item> items = new List<Item>();
		public Dictionary<string, Equipment> equipment = new Dictionary<string, Equipment>();

        public Human(int tileX, int tileY):base("Sprites/Human/humanonesprite",new Vector2(tileX*32,tileY*32),32,64,true, 1)
        {
            AddAnimationState(new SpriteState("walking",new Rectangle[]{GetRectangle(32, 64, 0, 1, 0),GetRectangle(32, 64, 0, 2, 0),GetRectangle(32, 64, 0, 3, 0)}, 10));
			AddAnimationState(new SpriteState("standing",new Rectangle[]{GetRectangle(32, 64, 0, 0, 0)}, 0));
			equipment["Hands"] = null;
			this.SetCurrentAnimationState("standing");
		}

		public void HandleInput(InputState inputState)
		{
			if (inputState.IsMouseLeftButtonPressed())
			{
				if (inputState.IsMouseOver(this))
				{
					isSelected = true;
				}
				else
				{
					isSelected = false;
				}
			}
			if (isSelected && inputState.IsMouseRightButtonPressed())
			{
				hasTask = true;
				goalPosition = inputState.MousePosition;
			}
		}
		public void MineTiles(List<Tile> tiles)
		{
			tilesToMine.AddRange(tiles);
			hasTask = true;
		}
		public void DoAction()
		{
			if (GameObjectsRegistry.tileMap.getTile((int)position.X / 32, (int)position.Y / 32 +2).type == Tile.Type.Air)
			{
				position.Y++;
			}
			if (!hasTask)
			{
               
			}
			else if (hasTask)
			{
				DoWalkTask();
				switch (currentTask)
				{
					case Task.mining: break;
				}
			}
		}
		private void DoWalkTask()
		{
			if (goalPosition.X < this.position.X)
			{
				this.position.X--;
				if (currentState == State.Standing)
				{
					SetCurrentAnimationState("walking");
					currentState = State.Walking;
				}
				if (facingRight)
				{
					this.spriteEffects = SpriteEffects.FlipHorizontally;
					facingRight = false;
				}
			}
			else if (goalPosition.X > this.position.X)
			{
				this.position.X++;
				if (currentState == State.Standing)
				{
					SetCurrentAnimationState("walking");
					currentState = State.Walking;

				}
				if (!facingRight)
				{
					this.spriteEffects = SpriteEffects.None;
					facingRight = true;
				}
			}
			else
			{
				hasTask = false;
				if (currentState == State.Walking)
				{
					SetCurrentAnimationState("standing");
					currentState = State.Standing;
				}
			}
		}
		private void DoMineTask()
		{
			if (GameMathHelper.DistanceBetweenTwoPoints(this.position, goalTile.position)<=32)
			{
				goalTile.Mine(equipment["Hands"]);
			}
		}
		private void Equip(string equipment)
		{
			
		}
    }
}
